<template>
	<div>
		<smpPanel>
			<el-form :inline="false" label-width="100px" size="mini" ref="form" :model="query.body">
				<el-row>
					  <el-col :span="6">
						<el-form-item label="角色名称:" prop="roleName">
							<el-input v-model="query.body.roleName"></el-input>
						</el-form-item>
					</el-col> 
					<!-- 	<el-col :span="6">
						<el-form-item label="ID:" prop="id">
							<el-input v-model="query.body.id"></el-input>
						</el-form-item>
					</el-col> -->


					<el-col :span="12">
						<searchButtonGroup @search="getList" @reset="reset" online />
					</el-col>
				</el-row>
				</el-row>
			</el-form>
		</smpPanel>

		<smpPanel title="角色列表">
			<createButton slot="option" @click="addRole" />
			<el-table :data="list" size="mini" stripe style="width: 100%">
				<el-table-column prop="id" label="ID" align="center">
				</el-table-column>
				<el-table-column prop="roleName" label="角色名称" align="center">
				</el-table-column>
				<el-table-column label="操作" align="center" width="200px">
					<template slot-scope="scope">
						<el-link size="mini" type="primary" :underline="false" style="font-size: 12px;"
							@click="handleEdit(scope.$index, scope.row)">修改</el-link>
						<el-divider direction="vertical"></el-divider>
						<!-- <el-link
				      size="mini
				    						 type="primary"
				    						 :underline="false"
				    						 style="font-size: 12px;"
				      @click="delrole(scope.$index, scope.row)">删除</el-link>/ -->
						<el-link size="mini" type="primary" :underline="false" style="font-size: 12px;"
							@click="editMenu(scope.$index, scope.row)">菜单权限</el-link>
						<el-divider direction="vertical"></el-divider>
						<el-link size="mini" type="primary" :underline="false" style="font-size: 12px;"
							@click="editPermission(scope.$index, scope.row)">角色权限</el-link>

					</template>
				</el-table-column>


			</el-table>
			<div class="smp-pagination" v-show="total>0">
				<el-pagination background size="mini" layout="total,sizes,prev, pager, next,jumper"
					:page-size="query.pageSize" :page-sizes="[5,10,15,20]" :total="total" :current-page="query.pageNum"
					@current-change="currentChange" @size-change="sizeChange">
				</el-pagination>
			</div>
		</smpPanel>


		<role ref="role" @fresh="fresh" />
		<roleEdit ref="roleEdit" @fresh="fresh" />
		<roleDel ref="roleDel" @fresh="fresh" />
		<editMenu ref="editMenu" />
		<editPermission ref="editPermission" />
	</div>
</template>

<script>
	"use strict";
	import {
		selectByPage
	} from '@/api/role.js';
	import role from "./components/role";
	import roleEdit from "./components/roleEdit";
	import roleDel from "./components/roleDel";
	import editMenu from "./components/editMenu";
	import editPermission from "./components/editPermission";
	export default {
		data() {
			return {
				list: [],
				query: {
					body: {
						roleName: "",
						id: ""
					},
					pageNum: 1,
					pageSize: 5
				},
				total: 0
			};
		},

		components: {
			role,
			roleEdit,
			roleDel,
			editMenu,
			editPermission
		},
		created() {
			Object.assign(this.query.body, this.$route.query);
			if (this.$route.query.pageNum != undefined) {
				this.query.pageNum = this.$route.query.pageNum;
			}
			if (this.$route.query.pageSize != undefined) {
				this.query.pageSize = parseInt(this.$route.query.pageSize);
			}
			this.getList();
		},
		methods: {

			addRole() {
				this.$refs.role.ShowDialog();
			},

			editMenu(index, row) {
				let row1 = {};
				Object.assign(row1, row);
				this.$refs.editMenu.ShowDialog(row1);
			},

			editPermission(index, row) {
				let row1 = {};
				Object.assign(row1, row);
				this.$refs.editPermission.ShowDialog(row1);
			},

			/**
			 * @param {Object} index
			 * @param {Object} row
			 */
			handleEdit(index, row) {
				this.$refs.roleEdit.ShowDialog(row);
			},

			/**
			 * @param {Object} index
			 * @param {Object} row
			 */
			delrole(index, row) {
				this.$refs.roleDel.ShowDialog(row);
			},


			getList() {
				selectByPage(this.query).then(response => {
					this.list = response.data.extend.list
					this.total = response.data.extend.total;

					let query = {};
					Object.assign(query, this.query.body);
					query.pageNum = this.query.pageNum;
					query.pageSize = this.query.pageSize;
					this.$router.push({
						query: query
					})
				}).catch(error => {
					console.info("失败");
					console.info(error.message);
				});
			},

			currentChange(val) {
				this.query.pageNum = val;
				this.getList();
			},
			sizeChange(val) {
				this.query.pageSize = val;
				this.query.pageNum = 1;
				this.getList();
			},
			fresh() {
				this.getList();
			},
			reset(){
				this.query = {
					body: {
						roleName: "",
						id: ""
					},
					pageNum: 1,
					pageSize: 5
				},
				this.getList();
			}

		}
	}
</script>

<style>
</style>
